export default {
  data() {
    return {
      minheight: "300px",
      host: "http://localhost:3000/", //主机地址
      model: "upload",
      list: [], //存储数据
      selectDataList: "",
      filter: {
        total: 0,
        page: 1,
        rows: 5
      },
      pageSizeOpts: [5, 10, 20, 30, 40]
    };
  },
  methods: {
    //密码显示按钮操作
    passwordShow() {
      if (this.passwordType == "password") {
        this.passwordType = "text";
        this.ios = "eye-disabled";
      } else if (this.passwordType == "text") {
        this.passwordType = "password";
        this.ios = "eye";
      }
    },
    show(index) {
      this.$Modal.info({
        title: "User Info",
        content: `Name：${this.list[index].name}<br>Age：${
          this.list[index].age
        }<br>Address：${this.list[index].address}`
      });
    },
    //grid  全选方法
    handleSelectAll(status) {
      //全选方法
      this.$refs.selection.selectAll(status);
    },
    remove(index) {
      this.list.splice(index, 1);
    },
    //grid 获取数据 getData
    getData: function(data,fn) {
      return new Promise(resolve => {
        let newData = {};

        //判断是否有外来数据，若有，则加入到newData中去
        if (data) {
          newData = data;
          console.log(data);
        } 
  
        //在newData中添加page和rows信息
        newData.page = this.filter.page;
        newData.rows = this.filter.rows;
        
        //判断是否又外来方法，若有则执行
        fn && fn();

        //获取表单数据
        this.axios({
          method: "post",
          url: `${this.host}${this.model}/list`,
          data: newData
        }).then(res => {
          this.list = res.data.rows;
          this.filter.page = res.data.page;
          this.filter.total = res.data.total;
          this.filter.rows = res.data.limit;
          resolve(this.list);
        });
      });
    },
    //弹出form框   数据新增
    handleAxiosSubmit(name) {
      
        let newData = { ...this.formValidate }; //将表单数据深度拷贝到newData中，以便删除_id时，不会影响到this.formValidate
        let method = "post";
        let url = `${this.host}${this.model}/data`;
        //根据——id判断是否新增，是则删除_id字段
        if (newData._id.trim().length > 0) {
        url = url + "/" + newData._id;
            method = "put";
        } else {
            delete newData._id;
        }
        
        //ajax操作
        this.axios({
            method,
            url,
            data: newData
        }).then(res => {
          console.log(res);
            this.formState = false;
            this.handleReset(name);
            this.getData();
        });
    },
    //formGrid  删除单条数据
    del(id) {
      //弹出框提示用户是否删除数据
      this.$Modal.confirm({
        title: "删除数据",
        content: "<p>真的要删除这条数据吗？</p>",
        onOk: () => {
          this.axios({
            method: "delete",
            url: `${this.host}${this.model}/data/${id}`
          }).then(res => {
            this.getData();
          });
        }
      });
    },
    //formGrid 多条数据删除
    delGrids() {
      if (this.selectDataList.trim().length <= 0) {
        return this.$Modal.warning({
          title: "警告",
          content: "未选中任何数据！"
        });
      }
      console.log(this.selectDataList);
      //弹出框提示用户是否删除数据
      this.$Modal.confirm({
        title: "删除数据",
        content: "<p>真的要删除这条数据吗？</p>",
        onOk: () => {
          this.axios({
            method: "post",
            url: `${this.host}${this.model}/data/removes`,
            data: {
              ids: this.selectDataList
            }
          }).then(res => {
            this.getData();
          });
        }
      });
    },
    //监听grid数据被选中情况，并将被选中的数据id加入到selectDataList中去
    onSelectionChange(selection) {
      let selectList = [];
      selection.forEach(ele => {
        selectList.push(ele._id);
      });
      this.selectDataList = selectList.join();
    },
    //搜索框
    seach() {
      let data = {
        page: this.filter.page,
        rows: this.filter.rows
      };
      data.seachKey = this.seachObj.seachName;
      data.seachVal = this.seachObj.seachVal;
 
      // if (data.seachKey.trim() == "") {
      //   this.$Modal.info({
      //       title: "请选择查找类型",
      //       content: "请选择查找类型",
      //     });
      // } else {
        this.getData(data);
      // }
    },
    //页面跳转
    changePage(page) {
      this.filter.page = page;
      this.getData();
    },
    //页面显示数据数量修改
    changePageSize(pageSize) {
      this.filter.rows = pageSize;
      this.getData();
    }
  }
};
